package com.eattravel.controller;

import com.eattravel.dto.PageInfoResult;
import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.Store;
import com.eattravel.service.CommodityColletionService;
import com.eattravel.service.StoreColletionService;
import com.eattravel.vo.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/collection")
public class CollectionConntroller {

    @Autowired
    CommodityColletionService commodityColletionService;

    @ResponseBody
    @RequestMapping("/commoditycollection")
    public PageInfoResult<Commodity> selectAllCommodityCollection(String userId,int currPage) {
        PageInfoResult<Commodity> pageParameter=new PageInfoResult<Commodity>();
        pageParameter.setCurrPage(currPage);
        pageParameter.setPageSize(8);
        pageParameter = commodityColletionService.selectAllCommodityCollection(pageParameter,userId);
        return pageParameter;
    }


    @ResponseBody
    @RequestMapping("/canclecommoditycollection")
    public ResultData cancleCommodityCollection(String userId, String commodityId) {
        ResultData resultData = new ResultData();

        int rs = commodityColletionService.cancleCommodityCollection(userId, commodityId);
        if (rs > 0) {
            resultData.setCode(0);
            resultData.setMessage("取消收藏成功");

        } else {
            resultData.setCode(1);
            resultData.setMessage("取消收藏失败");
        }
        return resultData;
    }

    @Autowired
    StoreColletionService storeColletionService;

    @ResponseBody
    @RequestMapping("/storecollection")
    public PageInfoResult<Store> selectAllStoreCollection(String userId,int currPage) {
        PageInfoResult<Store> pageParameter=new PageInfoResult<Store>();
        pageParameter.setCurrPage(currPage);
        pageParameter.setPageSize(3);
        pageParameter = storeColletionService.selectAllStoreCollection(pageParameter,userId);
        return pageParameter;
    }

    @ResponseBody
    @RequestMapping("/canclestorecollection")
    public ResultData cancleStoreCollection(String userId, String storeId) {
        ResultData resultData = new ResultData();
        System.out.println(userId + storeId);

        int rs = storeColletionService.cancleStoreCollection(userId, storeId);
        if (rs > 0) {
            resultData.setCode(0);
            resultData.setMessage("取消收藏成功");
        } else {
            resultData.setCode(1);
            resultData.setMessage("取消收藏失败");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping("/addtoshopcart")
    public ResultData addToshopCart(String userId, String commodityId) {
        ResultData resultData = new ResultData();

        int rs = commodityColletionService.addToShopCart(userId, commodityId);
        if (rs == 1) {
            resultData.setCode(1);
            resultData.setMessage("购物车中已经有了这个商品");
        } else {
            resultData.setCode(0);
            resultData.setMessage("添加成功");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping("/addtocoll")
    public ResultData addToColl(String userId, String storeId) {
        ResultData resultData = new ResultData();

        int rs = storeColletionService.addToColl(userId,storeId);
        if (rs == 1) {
            resultData.setCode(1);
            resultData.setMessage("已经收藏过这个商家");
        } else {
            resultData.setCode(0);
            resultData.setMessage("收藏成功");
        }
        return resultData;
    }

    @ResponseBody
    @RequestMapping("/commoditycollection1")
    public PageInfoResult<Commodity> selectAllCommodityCollection1(String userId,int currPage) {
        PageInfoResult<Commodity> pageParameter=new PageInfoResult<Commodity>();
        pageParameter.setCurrPage(currPage);
        pageParameter.setPageSize(3);
        pageParameter = commodityColletionService.selectAllCommodityCollection(pageParameter,userId);
        return pageParameter;
    }




}
